{% extends "zato/index.html" %}
{% load extras %}

{% block html_title %}Endpoint subscriptions{% endblock %}

{% block "extra_css" %}
    <link rel="stylesheet" type="text/css" href="/static/css/jquery.alerts.css">

    <style type="text/css">
        #data-table td {
            vertical-align:middle
        }
    </style>

{% endblock %}

{% block "extra_js" %}

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.tablesorter.min.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.cookie.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/jquery/jquery.alerts.min.js"></script>

    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/common.js"></script>
    <script nonce="{{ CSP_NONCE }}" type="text/javascript" src="/static/js/pubsub/endpoint-queues.js"></script>

    <script nonce="{{ CSP_NONCE }}">
    $.fn.zato.data_table.get_columns = function() {
        return [
            '_numbering',
            '_selection',

            '_name',
            'active_status',

            '_total_depth',

            '_creation_time',
            '_sub_key',
            '_ext_client_id',

            '_clear',
            '_edit',
            '_delete',

            {% comment %} -- 1 -- {% endcomment %}
            'id',
            'sub_key',
            'amqp_exchange',
            'amqp_routing_key',
            'creation_time',

            {% comment %} -- 2 -- {% endcomment %}
            'delivery_batch_size',
            'delivery_data_format',
            'delivery_endpoint',
            'delivery_max_retry',
            'delivery_method',

            {% comment %} -- 3 -- {% endcomment %}
            'endpoint_id',
            'endpoint_name',
            'endpoint_type',
            'ext_client_id',
            'files_directory_list',

            {% comment %} -- 4 -- {% endcomment %}
            'ftp_directory_list',
            'is_durable',
            'is_internal',
            'name',

            {% comment %} -- 5 -- {% endcomment %}
            'out_amqp_id',
            'out_http_method',
            'out_http_soap_id',
            'server_id',
            'server_name',

            {% comment %} -- 6 -- {% endcomment %}
            'service_id',
            'sms_twilio_from',
            'sms_twilio_to_list',
            'smtp_body',

            {% comment %} -- 7 -- {% endcomment %}
            'smtp_from',
            'smtp_is_html',
            'smtp_subject',
            'smtp_to_list',
            'topic_id',

            {% comment %} -- 8 -- {% endcomment %}
            'topic_name',
            'wrap_one_msg_in_list',
            'wait_sock_err',
            'wait_non_sock_err',
            'out_rest_http_soap_id',

            {% comment %} -- 9 -- {% endcomment %}
            'out_soap_http_soap_id',
            'delivery_err_should_block',
            'is_staging_enabled',
            'current_depth_gd',
            'current_depth_non_gd',
        ]
    }
    </script>

{% endblock %}

{% block "content" %}
<h2 class="zato">
    Subscription queues for endpoint :
    <a href="{% url "pubsub-endpoint" %}?cluster={{ cluster_id }}&amp;query={{ endpoint.name }}">{{ endpoint.name }}</a>
    : Type :
    {{ client_html|safe }}
</h2>

{% if not zato_clusters %}
    {% include "zato/no-clusters.html" %}
{% else %}

<div id="user-message-div" style='display:none'><pre id="user-message" class="user-message"></pre></div>

{% include "zato/search-form.html" with page_prompt="Show subscriptions"%}

    {% if cluster_id %}

        <div id="markup">
            <table id="data-table" style="vertical-align:middle">
                <thead>
                    <tr class='ignore'>
                        <th style="width:2%">&nbsp;</th>
                        <th>&nbsp;</th>

                        <th><a href="#">Sub queue for topic</a></th>
                        <th><a href="#">State</a></th>

                        <th><a href="#">Current depth GD / RAM</a></th>

                        <th><a href="#">Creation / State update</a></th>
                        <th style="min-width:260px"><a href="#">Sub key</a></th>
                        <th style="min-width:260px"><a href="#">EXTCLI</a></th>

                        <th style="width:5%">&nbsp;</th>
                        <th style="width:5%">&nbsp;</th>
                        <th style="width:5%">&nbsp;</th>

                        {% comment %} -- 1 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 2 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 3 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 4 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 5 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 6 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 7 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 8 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                        {% comment %} -- 9 -- {% endcomment %}
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>
                        <th class='ignore'>&nbsp;</th>

                </thead>

                <tbody>
                {% if items %}
                {% for item in items %}
                    <tr class="{% cycle 'odd' 'even' %}" id='tr_{{ item.sub_id }}'>
                        <td class='numbering'>&nbsp;</td>
                        <td class="impexp"><input type="checkbox" /></td>

                        <td>
                            <a href="{% url "pubsub-topic" %}?cluster={{ cluster_id }}&amp;query={{ item.topic_name }}">{{ item.topic_name }}</a>
                        </td>
                        <td>{{ item.active_status }}</td>

                        <td id="total_depth_{{ item.sub_id}}">
                            <a href="{% url "pubsub-endpoint-queue-browser" "true" item.sub_id item.topic_name|slugify %}?cluster={{ cluster_id }}">{{ item.current_depth_gd }}</a>
                            /
                            {{ item.current_depth_non_gd }}
                        </td>

                        <td style="width:20%">
                            <a
                                id="a_creation_time_{{ item.sub_id }}"
                                href="javascript:$.fn.zato.toggle_time('creation_time_{{ item.sub_id }}', '{{ item.creation_time }}', '{{ item.creation_time_utc }}')">{{ item.creation_time }}</a>
                            {% if item.last_interaction_time %}
                                <br/>
                                <a
                                    id="a_last_interaction_time_{{ item.sub_id }}"
                                    href="javascript:$.fn.zato.toggle_time('last_interaction_time_{{ item.sub_id }}', '{{ item.last_interaction_time }}', '{{ item.last_interaction_time_utc }}')">{{ item.last_interaction_time }}</a>
                            {% endif %}
                        </td>

                        <td>
                            {{ item.sub_key }}
                        </td>

                        <td>
                            {{ item.ext_client_id|no_value_indicator }}
                        </td>

                        <td><a href="javascript:$.fn.zato.pubsub.endpoint_queue.clear('{{ item.sub_id }}', '{{ cluster_id }}', '{{ item.topic_name }}')">Clear</a></td>
                        <td><a href="javascript:$.fn.zato.pubsub.endpoint_queue.edit('{{ item.sub_id }}')">Edit</a></td>
                        <td><a href="javascript:$.fn.zato.pubsub.endpoint_queue.delete_('{{ item.sub_id }}')">Delete</a></td>

                        {% comment %} -- 1 -- {% endcomment %}
                        <td class='ignore item_id_{{ item.sub_id }}'>{{ item.sub_id }}</td>
                        <td class='ignore'>{{ item.sub_key }}</td>
                        <td class='ignore'>{{ item.amqp_exchange|default:'' }}</td>
                        <td class='ignore'>{{ item.amqp_routing_key|default:'' }}</td>
                        <td class='ignore'>{{ item.creation_time|default:'' }}</td>

                        {% comment %} -- 2 -- {% endcomment %}
                        <td class='ignore'>{{ item.delivery_batch_size|default:'' }}</td>
                        <td class='ignore'>{{ item.delivery_data_format|default:'' }}</td>
                        <td class='ignore'>{{ item.delivery_endpoint|default:'' }}</td>
                        <td class='ignore'>{{ item.delivery_max_retry|default:'' }}</td>
                        <td class='ignore'>{{ item.delivery_method|default:'' }}</td>

                        {% comment %} -- 3 -- {% endcomment %}
                        <td class='ignore'>{{ item.endpoint_id|default:'' }}</td>
                        <td class='ignore'>{{ item.endpoint_name|default:'' }}</td>
                        <td class='ignore'>{{ item.endpoint_type|default:'' }}</td>
                        <td class='ignore'>{{ item.ext_client_id|default:'' }}</td>
                        <td class='ignore'>{{ item.files_directory_list|default:'' }}</td>

                        {% comment %} -- 4 -- {% endcomment %}
                        <td class='ignore'>{{ item.ftp_directory_list|default:'' }}</td>
                        <td class='ignore'>{{ item.is_durable }}</td>
                        <td class='ignore'>{{ item.is_internal }}</td>
                        <td class='ignore'>{{ item.name|default:'' }}</td>

                        {% comment %} -- 5 -- {% endcomment %}
                        <td class='ignore'>{{ item.out_amqp_id|default:'' }}</td>
                        <td class='ignore'>{{ item.out_http_method|default:'' }}</td>
                        <td class='ignore'>{{ item.out_http_soap_id|default:'' }}</td>
                        <td class='ignore'>{{ item.server_id|default:'' }}</td>
                        <td class='ignore'>{{ item.server_name|default:'' }}</td>

                        {% comment %} -- 6 -- {% endcomment %}
                        <td class='ignore'>{{ item.service_id|default:'' }}</td>
                        <td class='ignore'>{{ item.sms_twilio_from|default:'' }}</td>
                        <td class='ignore'>{{ item.sms_twilio_to_list|default:'' }}</td>
                        <td class='ignore'>{{ item.smtp_body|default:'' }}</td>

                        {% comment %} -- 7 -- {% endcomment %}
                        <td class='ignore'>{{ item.smtp_from|default:'' }}</td>
                        <td class='ignore'>{{ item.smtp_is_html }}</td>
                        <td class='ignore'>{{ item.smtp_subject|default:'' }}</td>
                        <td class='ignore'>{{ item.smtp_to_list|default:'' }}</td>
                        <td class='ignore'>{{ item.topic_id|default:'' }}</td>

                        {% comment %} -- 8 -- {% endcomment %}
                        <td class='ignore'>{{ item.topic_name|default:'' }}</td>
                        <td class='ignore'>{{ item.wrap_one_msg_in_list }}</td>
                        <td class='ignore'>{{ item.wait_sock_err|default:'' }}</td>
                        <td class='ignore'>{{ item.wait_non_sock_err|default:'' }}</td>
                        <td class='ignore'>{{ item.out_rest_http_soap_id|default:'' }}</td>

                        {% comment %} -- 9 -- {% endcomment %}
                        <td class='ignore'>{{ item.out_soap_http_soap_id|default:'' }}</td>
                        <td class='ignore'>{{ item.delivery_err_should_block }}</td>
                        <td class='ignore'>{{ item.is_staging_enabled }}</td>
                        <td class='ignore'>{{ item.current_depth_gd }}</td>
                        <td class='ignore'>{{ item.current_depth_non_gd }}</td>

                {% endfor %}
                {% else %}
                    <tr class='ignore'>
                        <td colspan='51'>No results</td>
                    </tr>
                {% endif %}

                </tbody>
            </table>
        </div>

        <div id="edit-div" class='data-popup ignore'>
            <div class="bd">
                <form novalidate action="{% url "pubsub-endpoint-queue-edit" %}" method="post" id="edit-form">
                    <table class="form-data">

                        {% if endpoint.endpoint_type == endpoint_type.WEB_SOCKETS.id %}

                        <tr>
                            <td style="vertical-align:middle; width:20%"><label for="id_edit-active_status">State</label></td>
                            <td>
                                {{ edit_form.active_status }}
                            </td>
                        </tr>

                        {% else %}

                        <tr>
                            <td style="vertical-align:middle; width:20%"><label for="id_edit-server_id">Delivery server</label></td>
                            <td>
                                {{ edit_form.server_id }}
                                | <label>State {{ edit_form.active_status }}</label>
                            </td>
                        </tr>

                        {% endif %}

                        {% if endpoint.endpoint_type != endpoint_type.WEB_SOCKETS.id and endpoint.endpoint_type != endpoint_type.SERVICE.id %}

                        <tr>
                            <td style="vertical-align:middle; width:12%"><label for="id_edit-delivery_method">Delivery method</label></td>
                            <td>
                                {{ edit_form.delivery_method }}
                            </td>
                        </tr>

                        {% endif %}

                        <tr>
                            <td style="vertical-align:middle; width:12%"><label for="id_edit-delivery_batch_size">Deliv. batch size</label></td>
                            <td>
                                {{ edit_form.delivery_batch_size }}
                                |
                                <label for="id_edit-wrap_one_msg_in_list">As list</label>
                                {{ edit_form.wrap_one_msg_in_list }}
                                |
                                <label for="id_edit-delivery_max_retry">Deliv. max retries</label>
                                {{ edit_form.delivery_max_retry }}
                            </td>
                        </tr>

                        <tr>
                            <td style="vertical-align:middle; width:12%">
                                <label for="id_edit-wait_sock_err">Sleep on error</label>
                                <br/>
                                <label for="id_edit-wait_non_sock_err">socket / other</label>
                            </td>
                            <td>
                                {{ edit_form.wait_sock_err }}
                                {{ edit_form.wait_non_sock_err }}
                                <span class="form_hint">(both in seconds)</span>
                            </td>
                        </tr>

                        {% comment %} --------- AMQP --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.AMQP.id %}
                        <tr id="dyn_form_row_id_out_amqp_id">
                            <td style="vertical-align:middle; width:12%">AMQP outconn</td>
                            <td>{{ edit_form.out_amqp_id }}</td>
                        </tr>
                        <tr id="dyn_form_row_id_amqp_details">
                            <td style="vertical-align:middle; width:12%">AMQP exchange<br/>and routing key</td>
                            <td>
                                {{ edit_form.amqp_exchange }}
                                {{ edit_form.amqp_routing_key }}
                            </td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- Files --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.FILES.id %}
                        <tr id="dyn_form_row_id_files_directory_list">
                            <td style="vertical-align:middle; width:12%">Local directories</td>
                            <td>{{ edit_form.files_directory_list }}</td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- FTP --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.FTP.id %}
                        <tr>
                        <tr id="dyn_form_row_id_ftp_directory_list">
                            <td style="vertical-align:middle; width:12%">FTP directories</td>
                            <td>{{ edit_form.ftp_directory_list }}</td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- REST/SOAP --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.REST.id %}
                        <tr id="dyn_form_row_id_out_http_method">
                            <td style="vertical-align:middle; width:12%"><label for="id_edit-out_http_method">HTTP method</label></td>
                            <td>{{ edit_form.out_http_method }}</td>
                        </tr>
                        {% endif %}
                        {% if endpoint.endpoint_type == endpoint_type.SOAP.id %}
                        <tr id="dyn_form_row_id_out_http_method">
                            <td style="vertical-align:middle; width:12%"><label for="id_edit-out_http_method">HTTP method</label></td>
                            <td>{{ edit_form.out_http_method }}</td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- REST --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.REST.id %}
                        <tr id="dyn_form_row_id_out_rest_http_soap_id">
                            <td style="vertical-align:middle; width:12%"><label for="id_edit-out_rest_http_soap_id">REST connection</label></td>
                            <td>{{ edit_form.out_rest_http_soap_id }}</td>
                        </tr>
                        <tr id="dyn_form_row_id_rest_delivery_endpoint" style="display:none">
                            <td style="vertical-align:middle; width:12%">REST callback</td>
                            <td>{{ edit_form.rest_delivery_endpoint }}</td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- Service ---------
                        {% if endpoint.endpoint_type == endpoint_type.SERVICE.id %}
                        <tr id="dyn_form_row_id_service_id">
                            <td style="vertical-align:middle"><label for="id_edit-service_id">Service</label></td>
                            <td>{{ edit_form.service_id }} </td>
                        </tr>
                        {% endif %}
                        {% endcomment %}

                        {% comment %} --------- SMS - Twilio --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.SMS_TWILIO.id %}
                        <tr id="dyn_form_row_id_sms_twilio_from">
                            <td style="vertical-align:middle">SMS from</td>
                            <td>{{ edit_form.sms_twilio_from }} </td>
                        </tr>
                        <tr id="dyn_form_row_id_sms_twilio_to_list">
                            <td style="vertical-align:middle">SMS to</td>
                            <td>{{ edit_form.sms_twilio_to_list }} </td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- SMTP --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.SMTP.id %}
                        <tr id="dyn_form_row_id_out_smtp_id">
                            <td style="vertical-align:middle">SMTP outconn</td>
                            <td>
                                {{ edit_form.out_smtp_id }}
                                |
                                <label>
                                HTML? {{ create_form.smtp_is_html }}
                                </label>
                            </td>
                        </tr>
                        <tr id="dyn_form_row_id_smtp_subject">
                            <td style="vertical-align:middle">SMTP subject</td>
                            <td>
                                {{ edit_form.smtp_subject }}
                            </td>
                        </tr>
                        <tr id="dyn_form_row_id_smtp_from">
                            <td style="vertical-align:middle">SMTP from</td>
                            <td>
                                {{ edit_form.smtp_from }}
                            </td>
                        </tr>
                        <tr id="dyn_form_row_id_smtp_to_list">
                            <td style="vertical-align:middle">SMTP to</td>
                            <td>
                                {{ edit_form.smtp_to_list }}
                            </td>
                        </tr>
                        <tr id="dyn_form_row_id_smtp_body">
                            <td style="vertical-align:middle">SMTP body</td>
                            <td>
                                {{ edit_form.smtp_body }}
                            </td>
                        </tr>
                        {% endif %}

                        {% comment %} --------- SOAP --------- {% endcomment %}
                        {% if endpoint.endpoint_type == endpoint_type.SOAP.id %}
                        <tr id="dyn_form_row_id_out_soap_http_soap_id">
                            <td style="vertical-align:middle; width:12%">SOAP outconn</td>
                            <td>{{ edit_form.out_soap_http_soap_id }}</td>
                        </tr>
                        <tr id="dyn_form_row_id_soap_delivery_endpoint" style="display:none">
                            <td style="vertical-align:middle; width:12%">SOAP callback</td>
                            <td>{{ edit_form.soap_delivery_endpoint }}</td>
                        </tr>
                        {% endif %}

                        <tr>
                            <td colspan="2" style="text-align:right">
                                <input type="hidden" id="id_edit-name" name="edit-name" />
                                <input type="hidden" id="id_edit-topic_name" name="edit-topic_name" />
                                <input type="submit" value="OK" />
                                <button type='button' onclick='javascript:$.fn.zato.data_table.close(this)'>Cancel</button>
                            </td>
                        </tr>

                    </table>
                    {{ edit_form.sub_key }}
                    <input type="hidden" id="id_edit-endpoint_type" name="endpoint_type" value="{{ endpoint.endpoint_type }}" />
                    <input type="hidden" id="id_edit-cluster_id" name="cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="cluster_id" name="_name_unused_cluster_id" value="{{ cluster_id }}" />
                    <input type="hidden" id="id_edit-id" name="id" />
                </form>
            </div>
        </div>

{% include "zato/paginate/action-panel.html" %}

    {% endif %} {% comment %}cluster_id{% endcomment %}

{% endif %}{% comment %}not zato_clusters{% endcomment %}

{% endblock %}
